/**
 * Created by Liujx on 2017-10-22 22:38:21
 */
const detailUrl = require('../../config').detailUrl;
const addCartUrl = require('../../config').addCartUrl;
const CommentListUrl = require('../../config').CommentListUrl;
const util = require('../../utils/util.js');
const uploadFileUrl = require('../../config.js').uploadFileUrl;
const CreateListUrl = require('../../config.js').CreateListUrl;
var playTimeInterval;
var recordTimeInterval;
var user_nick = wx.getStorageSync('user_nickname');
var user_head = wx.getStorageSync('user_head');
var WxParse = require('../../wxParse/wxParse.js');
Page({
	data: {
		loadmore: true,
		detailList: [],
    recording: false,
    playing: false,
    hasRecord: false,
    recordTime: 0,
    playTime: 0,
    seq: 1,
    formatedRecordTime: '00:00:00',
    formatedPlayTime: '00:00:00',   
    voiceKey: '',	// 音频key
    post_id:'',  
 	},
 	onLoad: function(options) {     
 		var that = this;    
	  wx.request({
		  url: detailUrl,
		  method: 'GET',
		  data: {
		     id: options.id
		  },
		  header: {
		      'content-type': 'application/x-www-form-urlencoded' // 默认值
		  },
		  success: function(res) {
        //console.log(user_nick)
        console.log(res)
        that.data.post_id = res.data.data.id
		  	that.setData({
		  		loadmore: false,
          //detailList: [res.data.data],
          published_time: res.data.data.published_time,
          post_title: res.data.data.post_title,
          post_content: WxParse.wxParse('post_content', 'html', res.data.data.post_content, that, 5)         
		  	})
		  }
		})
    that.getCommentListRequest(options.id); //666，活学活用传参option
	},
  onReady: function () {
      
  },
	onShow: function() {
		wx.setNavigationBarTitle({
		  title: '正文'
		})
	},
  tapRecord: function () {
    wx.navigateTo({
      url: '../create/create'
    })
  },
  getCommentListRequest: function (id) {     //666，活学活用传参option
    var that = this
    //var post_id = that.data.post_id;
    console.log(id)    
    wx.request({
      url: CommentListUrl,
      method: 'GET',
      data: {
          id:id,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        console.log(res)
        that.setData({
          loadmore: false,
          commentList: res.data.data
        })
      }
    })

  },

	addCartFun: function(e) {
		var that = this;
		var bookId = e.currentTarget.dataset.bookid;
		var bookListId = e.currentTarget.dataset.booklistid;
	    wx.request({
		  url: addCartUrl,
		  method: 'POST',
		  data: {
		    bookId: bookId,
		    bookListItemId: bookListId,
		    fromShareId: ''
		  },
		  header: {
		    'content-type': 'application/x-www-form-urlencoded' // 默认值
		  },
		  success: function(res) {
			console.log(res);
		  }
		})
	},
  onHide: function () {
    if (this.data.playing) {
      this.stopVoice()
    } else if (this.data.recording) {
      this.stopRecordUnexpectedly()
    }
  }, 
  startRecord: function () {
    this.setData({ recording: true })
    var that = this
    recordTimeInterval = setInterval(function () {
      var recordTime = that.data.recordTime += 1
      that.setData({
        formatedRecordTime: util.formatTime(that.data.recordTime),
        recordTime: recordTime
      })
    }, 1000)
    wx.startRecord({
      success: function (res) {
        that.setData({
          hasRecord: true,
          tempFilePath: res.tempFilePath,
          formatedPlayTime: util.formatTime(that.data.playTime)
        })
        wx.uploadFile({
          url: uploadFileUrl,
          filePath: res.tempFilePath,
          name: 'file',
          success: function (res) {
            console.log(res)
            that.data.voiceKey = JSON.parse(res.data).data;   //字符的需要解码
          },
          fail: function (res) {
            util.showMessage(that, "服务器端错误！", 3000);
          }
        })
      },
      complete: function () {
        that.setData({ recording: false })
        clearInterval(recordTimeInterval)
      }
    })
  },
  stopRecord: function () {
    wx.stopRecord()
  },
  stopRecordUnexpectedly: function () {
    var that = this
    wx.stopRecord({
      success: function () {
        clearInterval(recordTimeInterval)
        that.setData({
          recording: false,
          hasRecord: false,
          recordTime: 0,
          formatedRecordTime: util.formatTime(0)
        })
      }
    })
  },
  playVoice: function () {
    var that = this
    playTimeInterval = setInterval(function () {
      var playTime = that.data.playTime + 1
      that.setData({
        playing: true,
        formatedPlayTime: util.formatTime(playTime),
        playTime: playTime
      })
    }, 1000)
    wx.playVoice({
      filePath: this.data.tempFilePath,
      success: function () {
        clearInterval(playTimeInterval)
        var playTime = 0
        that.setData({
          playing: false,
          formatedPlayTime: util.formatTime(playTime),
          playTime: playTime
        })
      }
    })
  },
  pauseVoice: function () {
    clearInterval(playTimeInterval)
    wx.pauseVoice()
    this.setData({
      playing: false
    })
  },
  stopVoice: function () {
    clearInterval(playTimeInterval)
    this.setData({
      playing: false,
      formatedPlayTime: util.formatTime(0),
      playTime: 0
    })
    wx.stopVoice()
  },
  clear: function () {
    clearInterval(playTimeInterval)
    wx.stopVoice()
    this.setData({
      playing: false,
      hasRecord: false,
      tempFilePath: '',
      formatedRecordTime: util.formatTime(0),
      recordTime: 0,
      playTime: 0
    })
  },  
  submitFun: function () {
    var that = this;
    //let bookListName = that.data.bookListName;
    //let videoKey = that.data.videoKey;
    var voiceKey = that.data.voiceKey;
    var post_id = that.data.post_id;
    //let imagesKey = that.data.imagesKey;
    //let content = that.data.content;
    //let bookKey = that.data.bookKey;

    wx.request({
      url: CreateListUrl,
      method: 'GET',
      data: {
        "src": voiceKey ,
        "user_nick": user_nick,
        "user_head": user_head,
        "post_id":post_id
        //"items": bookKey
      },
      success: function (result) {
        console.log(result);
        util.showMessage(that, '朗读成功！', 2000);
      },
      fail: function ({ errMsg }) {
        console.log('request fail', errMsg)
        self.setData({
          loading: false
        })
      }
    })
  },
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {      
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  }
})